﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace stuManage
{
    public partial class inputGrade : System.Web.UI.Page
    {
        string teaid;
        string id;
        int failNum =0;
        int scoreSum=0;
        int num = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
             teaid= Request.QueryString["teaid"];
            id = Request.QueryString["id"];
            string  className = Request.QueryString["name"];

            string HTML1 = "<p> 课程编号：" + id+"&nbsp;&nbsp;课程名称："+ className;
            classInfo.InnerHtml = HTML1;

            string strConn = "server=localhost;port=3306;user id=root;password=lxy1756431734;database=student; pooling=true;";
            MySqlConnection conn = new MySqlConnection(strConn);
            conn.Open();//链接数据库  
            DataSet ds = new DataSet();
            string sql = "select name,department from teacher_inf where id = '" + teaid + "'";
            MySqlCommand com = new MySqlCommand(sql, conn);
            MySqlDataReader dr = com.ExecuteReader();
            dr.Read();
            string name = dr["name"].ToString();
            string department = dr["department"].ToString();

            string HTML2 = "<br/><p> 职工号：" + id + "<br/>姓名：" + name + "<br/>学院：" + department + "</p>";
            information.InnerHtml = HTML2;


            if (!IsPostBack)
            {
                showInfo();
            }

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                    TextBox tb = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]);
                int grade = Convert.ToInt32(tb.Text.Trim());
                if (grade<=0 || grade > 100)
                {
                    Response.Write(" <script>function window.onload() {alert( ' 分数在1——100之间' ); } </script> ");
                }
                else
                {
                    string sqltxt = "update stu_course set grade='" + grade + "' where stu_id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "' and course_id='" + id + "'";
                    string strConn = "server=localhost;port=3306;user id=root;password=lxy1756431734;database=student; pooling=true;";
                    MySqlConnection conn = new MySqlConnection(strConn);
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand(sqltxt, conn);
                    cmd.ExecuteNonQuery();

                }

            }
            catch
            {

            }
            this.GridView1.EditIndex = -1;
            showInfo(); //  绑定控件显示最后结果
        }

        private void showInfo()
        {
            string commandString = "SELECT stu_id,name,department,className,grade from stu_course,student_inf where course_id='" + id + "' and stu_id=student_inf.id ";
            string strConn = "server=localhost;port=3306;user id=root;password=lxy1756431734;database=student; pooling=true;";
            MySqlConnection conn = new MySqlConnection(strConn);
            conn.Open();//链接数据库  
            DataSet ds = new DataSet();

            MySqlDataAdapter da = new MySqlDataAdapter(commandString, conn);
            da.Fill(ds, "table");

            GridView1.DataSource = ds.Tables["table"];
            GridView1.DataBind();
        }

        

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            showInfo();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            showInfo();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("teaIndex.aspx?id=" + teaid);

        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
                string lbl = DataBinder.Eval(e.Row.DataItem, "grade").ToString();
                if (lbl.Length > 0)
                {
                    int grade = Convert.ToInt32(lbl);
                    if (grade < 60)
                    {
                        failNum++;
                        //e.Row.BackColor = Color.Red ; //行颜色
                        e.Row.Cells[4].ForeColor = Color.Red;
                    }
                    else
                        e.Row.Cells[4].ForeColor = Color.LightGreen;
                    scoreSum += grade;
                    num++;

                }

            }
            double avarage = 0;
            if (num != 0)
                avarage = scoreSum / num;
            String HTML3 = "平均成绩：" + avarage + "&nbsp;&nbsp;&nbsp;&nbsp;不及格人数：" + failNum;
            Statistics.InnerHtml = HTML3;


        }
    }
    }